﻿namespace SLClient.CustomerControls
{
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Input;
    using System.Collections.ObjectModel;

    public class BonusEditViewModel : ViewModelBase
    {
        private Visibility _complateVisiblity;
        private decimal _currentValue;
        private decimal _fandian;
        private decimal _jiangjin;
        private Visibility _loadingVisibility;
        private Visibility _mainVisiblity;
        private decimal _maxValue;
        private string _message;
        private int _playTypeId;
        private int _playTypeInfoId;
        private decimal _scaleBase;
        private string format;

        public BonusEditViewModel(int playTypeId, int playTypeInfoId, decimal jiangjin, decimal fandian)
        {
            Func<PlayTypeInfo, bool> func = null;
            this._playTypeId = playTypeId;
            this._jiangjin = jiangjin;
            this._fandian = fandian;
            this._playTypeInfoId = playTypeInfoId;
            if (func == null)
            {
                func = new Func<PlayTypeInfo, bool>((s) => { return (s.PlayInfoId == this._playTypeInfoId); });
            }
            this._scaleBase = Enumerable.First<PlayTypeInfo>(ViewDispatcher.PlayTypeInfos, func).ScaleBase;
            this.format = ((this._jiangjin / 2M) > 100M) ? "F0" : (((this._jiangjin / 2M) > 10M) ? "F1" : "F2");
            this._currentValue = this._jiangjin;
            this.CommandInit();
            this._maxValue = this._jiangjin + (this._scaleBase * this._fandian);
            this._complateVisiblity = Visibility.Collapsed;
            this._loadingVisibility = Visibility.Collapsed;
            this._mainVisiblity = Visibility.Visible;
        }

        public void CommandInit()
        {
            RelayCommand command = new RelayCommand(new Action(this.Decreas)) {
                IsEnabled = true
            };
            this.DecreaseBonuseCommand = command;
            RelayCommand command2 = new RelayCommand(new Action(this.Increase)) {
                IsEnabled = true
            };
            this.IncreaseBonuseCommand = command2;
            RelayCommand command3 = new RelayCommand(new Action(this.Edit)) {
                IsEnabled = true
            };
            this.EditCommand = command3;
        }

        public void Decreas()
        {
            if ((this.CurrentValue - this.Increament) >= this.MinValue)
            {
                this.CurrentValue -= this.Increament;
            }
        }

        public void Edit()
        {
            this.LoadingVisibility = Visibility.Visible;
            this.MainVisiblity = Visibility.Collapsed;
            UserScaleInfo userScaleInfo = new UserScaleInfo {
                UserId = ViewDispatcher.LoginUser.UserId,
                JiangJin = this.Jiangjin,
                FianDian = this.Fandian,
                PlayTypeInfoId = this._playTypeInfoId,
                PlayTypeId = this._playTypeId
            };
            ServiceClient serviceClient = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            serviceClient.GetUserScaleInfosByIdCompleted += delegate (object obj, GetUserScaleInfosByIdCompletedEventArgs arg) {
                new IsolatedStorageHelper<ObservableCollection<UserScaleInfo>>().CreateSettingsInStorage("UserScaleInfos", arg.Result);
                this.Message = "修改成功";
                this.LoadingVisibility = Visibility.Collapsed;
                this.CompleteVisiblity = Visibility.Visible;
                this.MainVisiblity = Visibility.Collapsed;
            };
            serviceClient.UpdateUserScaleInfoCompleted += delegate (object obj, UpdateUserScaleInfoCompletedEventArgs arg) {
                Func<UserScaleInfo, bool> func = null;
                if (arg.Error == null)
                {
                    if (func == null)
                    {
                        func = new Func<UserScaleInfo, bool>((s) => { return (s.PlayTypeId == userScaleInfo.PlayTypeId); });
                    }
                    List<UserScaleInfo> list = Enumerable.ToList<UserScaleInfo>(Enumerable.Where<UserScaleInfo>(ViewDispatcher.UserScaleInfos, func));
                    if (list.Count != 0)
                    {
                        foreach (UserScaleInfo info in list)
                        {
                            info.JiangJin = userScaleInfo.JiangJin;
                            info.FianDian = userScaleInfo.FianDian;
                        }
                        this.Message = "修改成功";
                        this.LoadingVisibility = Visibility.Collapsed;
                        this.CompleteVisiblity = Visibility.Visible;
                        this.MainVisiblity = Visibility.Collapsed;
                    }
                    else
                    {
                        serviceClient.GetUserScaleInfosByIdAsync(ViewDispatcher.LoginUser.UserId);
                    }
                }
                else
                {
                    this.Message = "修改失败";
                    this.LoadingVisibility = Visibility.Collapsed;
                    this.CompleteVisiblity = Visibility.Visible;
                    this.MainVisiblity = Visibility.Collapsed;
                }
            };
            serviceClient.UpdateUserScaleInfoAsync(userScaleInfo);
        }

        public void Increase()
        {
            if ((this.CurrentValue + this.Increament) <= this.MaxValue)
            {
                this.CurrentValue += this.Increament;
            }
        }

        public string BonusMessage
        {
            get
            {
                decimal num = this.Jiangjin / 2M;
                return string.Format("{0}/{1}", this.Jiangjin.ToString(this.format), num.ToString(this.format));
            }
        }

        public Visibility CompleteVisiblity
        {
            get
            {
                return this._complateVisiblity;
            }
            set
            {
                this._complateVisiblity = value;
                base.Notify<Visibility>(() => this.CompleteVisiblity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusEditViewModel)), (MethodInfo)(BonusEditViewModel.get_CompleteVisiblity)), new ParameterExpression[0]));
            }
        }

        public decimal CurrentValue
        {
            get
            {
                return this._currentValue;
            }
            set
            {
                value -= value % this.Increament;
                this._currentValue = value;
                this.Jiangjin = value;
                this.Fandian = (this.MaxValue - this._currentValue) / this._scaleBase;
                base.Notify<string>(() => BonusMessage);
                base.Notify<decimal>(() => Jiangjin);
                base.Notify<decimal>(() => Fandian);
                base.Notify<decimal>(() => CurrentValue);                
            }
        }

        public ICommand DecreaseBonuseCommand { get; set; }

        public ICommand EditCommand { get; set; }

        public decimal Fandian
        {
            get
            {
                return this._fandian;
            }
            set
            {
                this._fandian = value;
                base.Notify<decimal>(() => this.Fandian);
                //base.Notify<decimal>(System.Linq.Expressions.Expression.Lambda<Func<decimal>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusEditViewModel)), (MethodInfo)(BonusEditViewModel.get_Fandian)), new ParameterExpression[0]));
            }
        }

        public decimal Increament
        {
            get
            {
                return (this._scaleBase * 0.005M);
            }
        }

        public ICommand IncreaseBonuseCommand { get; set; }

        public decimal Jiangjin
        {
            get
            {
                return this._jiangjin;
            }
            set
            {
                this._jiangjin = value;
                base.Notify<decimal>(() => Jiangjin);
                //base.Notify<decimal>(System.Linq.Expressions.Expression.Lambda<Func<decimal>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusEditViewModel)), (MethodInfo)methodof(BonusEditViewModel.get_Jiangjin)), new ParameterExpression[0]));
            }
        }

        public Visibility LoadingVisibility
        {
            get
            {
                return this._loadingVisibility;
            }
            set
            {
                this._loadingVisibility = value;
                base.Notify<Visibility>(() => LoadingVisibility);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusEditViewModel)), (MethodInfo)methodof(BonusEditViewModel.get_LoadingVisibility)), new ParameterExpression[0]));
            }
        }

        public Visibility MainVisiblity
        {
            get
            {
                return this._mainVisiblity;
            }
            set
            {
                this._mainVisiblity = value;
                base.Notify<Visibility>(() => MainVisiblity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusEditViewModel)), (MethodInfo)(BonusEditViewModel.get_MainVisiblity)), new ParameterExpression[0]));
            }
        }

        public decimal MaxValue
        {
            get
            {
                return this._maxValue;
            }
        }

        public string Message
        {
            get
            {
                return this._message;
            }
            set
            {
                this._message = value;
                base.Notify<string>(() => this.Message);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(BonusEditViewModel)), (MethodInfo)(BonusEditViewModel.get_Message)), new ParameterExpression[0]));
            }
        }

        public decimal MinValue
        {
            get
            {
                return (this._scaleBase * 0.8M);
            }
        }
    }
}

